package kyssion.leetcode.num151_200;

public class code190_颠倒二进制位 {
    public static void main(String[] args) {
        System.out.println(new code190_颠倒二进制位().reverseBits(0));
    }

    public int reverseBits(int n) {
        int speed = 0;
        int before = n;
        int item = 0;
        while (speed < 32) {
            n = n >> 1;
            item = (item << 1) + (before - (n << 1));
            before = n;
            speed++;
        }
        return item;
    }

    // 二进制使用1& x 就能得到最后一位的值
    public int reverseBits2(int n) {
        int res = 0;
        for (int i = 0; i < 32; i++) {
            res += (1 & (n >> i)) << (31 - i);
        }
        return res;
    }
}
